﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLZJFurnitureSCM.Model;
using System.Data.Linq;
using System.Data;
using System.Data.SqlClient;

namespace QLZJFurnitureSCM.DAL
{
    public class EmployeeDAO : IBaseDAO<Employee>
    {
        private string connectionString = DALFactory.CONNECTIONSTRING;

        private SqlConnection sqlConn = null;

        public EmployeeDAO()
        {
            sqlConn = new SqlConnection(connectionString);
        }

        public Employee Create(Employee t)
        {
            try
            {
                string query = string.Format("INSERT INTO Employees(employeeName,employeeSex,employeeAge,employeePID,employeeDateCheckIn,employeePayNow,employeeCanDrive,employeePhone,employeeAddress)VALUES('{0}','{1}',{2},'{3}','{4}',{5},{6},'{7}','{8}'); select @@identity;"
                    , t.EmployeeName, t.EmployeeSex, t.EmployeeAge, t.EmployeePID, t.EmployeeDateCheckIn, t.EmployeePayNow, t.EmployeeCanDrive ? 1 : 0, t.EmployeePhone, t.EmployeeAddress);
                Console.WriteLine(query);
            if (sqlConn.State != ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand sqlCmd = new SqlCommand(query, sqlConn);
                object o = sqlCmd.ExecuteScalar();
                sqlConn.Close();
                if (o != null)
                    t.EmployeeId = int.Parse(o.ToString());
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                Console.WriteLine(ex);
                return Create(t);
            }
            return t;
        }

        public Employee Read(Employee t)
        {
            throw new NotImplementedException();
        }

        public bool Update(Employee t)
        {
            try
            {
                string query = string.Format("update Employees set employeeSex = '{0}', employeeAge = {1}, employeePID = '{2}', employeePayNow = {3}, employeeCanDrive = {4}, employeePhone = '{5}', employeeAddress = '{6}', employeeDateCheckIn = '{7}' where employeeId = {8};"
                    , t.EmployeeSex, t.EmployeeAge, t.EmployeePID, t.EmployeePayNow, t.EmployeeCanDrive ? 1 : 0, t.EmployeePhone, t.EmployeeAddress, t.EmployeeDateCheckIn, t.EmployeePID);
                Console.WriteLine("Update Query: " + query);
                DataContext dataContext = new DataContext(connectionString);
                dataContext.ExecuteQuery<Contact>(query);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }

        public bool Delete(Employee t)
        {
            try
            {
                string query = string.Format("delete Employees where employeeId = {0};", t.EmployeeId);
                Console.WriteLine("Update Query: " + query);
                DataContext dataContext = new DataContext(connectionString);
                dataContext.ExecuteQuery<Employee>(query);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }

        public List<Employee> ReadAll()
        {
            DataContext dataContext = new DataContext(connectionString);
            IEnumerable<Employee> enumEmloyee = dataContext.ExecuteQuery<Employee>("select * from Employees;");

            if (null == enumEmloyee)
                return null;
            return enumEmloyee.ToList();
        }
    }
}
